package croe.jdbc.utils;

import croe.jdbc.domain.SQL;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * User: zengzhili
 * Time: 2014-06-07 00:00
 */
@SuppressWarnings("unchecked")
public class ClassMethodUtil {

    private static final String TABLE_ID = "id";
    private static final String TABLE_PK_ID = "pk_id";

    public static SQL getSQL(Object obj) throws Exception {
        SQL sql = new SQL();
        List<String> columnName = new ArrayList<String>();
        List<Object> paramValue = new ArrayList<Object>();

        Field[] fields = obj.getClass().getDeclaredFields();
        boolean setPkId = true;
        List<Class> columnType = new ArrayList<Class>();
        for (Field field : fields) {
            String fieldName = field.getName();
            Object fileValue = getProperty(obj, fieldName);

            columnName.add(CamelCaseUtils.toUnderlineName(fieldName));
            paramValue.add(fileValue);

            columnType.add(field.getType());
            //����
            if (setPkId) {
                if (TABLE_PK_ID.equalsIgnoreCase(fieldName) || TABLE_ID.equalsIgnoreCase(fieldName)) {
                    sql.setPrimaryKey(CamelCaseUtils.toUnderlineName(fieldName));
                    sql.setPrimaryKeyValue(fileValue);
                    setPkId = false;
                }
            }
        }
        sql.setCloumnName(columnName);
        sql.setParamsValue(paramValue);
        sql.setTableName(getTableName(obj));
        sql.setCloumnType(columnType);
        return sql;
    }

    public static SQL getUpdateSQL(Object obj) throws Exception {
        SQL sql = new SQL();
        List<String> columnName = new ArrayList<String>();
        List<Object> paramValue = new ArrayList<Object>();

        Field[] fields = obj.getClass().getDeclaredFields();
        boolean setPkId = true;
        List<Class> columnType = new ArrayList<Class>();
        Field pkField = null;
        for (Field field : fields) {
            String fieldName = field.getName();
            Object fileValue = getProperty(obj, fieldName);
            //����
            if (setPkId&&(TABLE_PK_ID.equalsIgnoreCase(fieldName) || TABLE_ID.equalsIgnoreCase(fieldName))) {
                    sql.setPrimaryKey(CamelCaseUtils.toUnderlineName(fieldName));
                    sql.setPrimaryKeyValue(fileValue);
                    setPkId = false;
                    pkField = field;
            }else {

                columnName.add(CamelCaseUtils.toUnderlineName(fieldName));
                paramValue.add(fileValue);
                columnType.add(field.getType());
            }
        }
        //����pram��ColumnType
        columnName.add(sql.getPrimaryKey());
        paramValue.add(sql.getPrimaryKeyValue());
        assert pkField != null;
        columnType.add(pkField.getType());
        //----------------------
        sql.setCloumnName(columnName);
        sql.setParamsValue(paramValue);
        sql.setTableName(getTableName(obj));
        sql.setCloumnType(columnType);
        return sql;
    }



    public static SQL getSQL(Class classes) throws Exception {
        SQL sql = new SQL();
        List<String> columnName = new ArrayList<String>();
        Field[] fields = classes.getDeclaredFields();
        boolean setPkId = true;
        List<Class> columnType = new ArrayList<Class>();
        for (Field field : fields) {
            String fieldName = field.getName();
            columnName.add(CamelCaseUtils.toUnderlineName(fieldName));
            //����
            columnType.add(field.getType());
            if (setPkId) {
                if (TABLE_PK_ID.equalsIgnoreCase(fieldName) || TABLE_ID.equalsIgnoreCase(fieldName)) {
                    sql.setPrimaryKey(CamelCaseUtils.toUnderlineName(fieldName));
                    setPkId = false;
                }
            }
        }
        sql.setCloumnName(columnName);
        sql.setTableName(getTableName(classes));
        sql.setCloumnType(columnType);
        return sql;
    }

    public static String getTableName(Object obj) {
        return getTableName(obj.getClass());
    }

    public static String getTableName(Class classes) {
        return CamelCaseUtils.toUnderlineName(classes.getSimpleName());
    }

    private static Object getProperty(Object obj, String fieldName) {
        fieldName = CamelCaseUtils.firstCharToUpperCase(fieldName);
        try {
            Method method = obj.getClass().getMethod("get" + fieldName);
            return method.invoke(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



}
